﻿
using Family.Core.Model;
using Family.Data.Factories;
using NHibernate;
using NUnit.Framework;
using System.Collections.Generic;

namespace Family.Tests.Infrastructure
{
    [TestFixture]
    [Category("inmemory tests")]
    public abstract class NHTestBase : NHMemoryData
    {
        private readonly NHFactory factory = new TestFactory("inmemory");
        
        protected ITransaction transaction;

        protected ISession session;

        private void SaveTestedData()
        {
            SaveList(Categories);
            SaveList(Products);
        }

        [TestFixtureSetUp]
        public virtual void FixtureSetUp()
        {
            factory.Init();
            session = factory.OpenSession();

            //SaveTestedData();

            //session.Flush();
        }

        [TestFixtureTearDown]
        public void FixtureTearDown()
        {
            session.Close();
        }

        [SetUp]
        public virtual void Setup()
        {
            transaction = session.BeginTransaction();
        }

        [TearDown]
        public void TeaDown()
        {
            transaction.Rollback();
            session.Clear();
        }

        protected void SaveList(IEnumerable<EntityWithTypeId<int>> list)
        {
            foreach(var item in list)
            {
                session.SaveOrUpdate(item);
            }
        }


    }
}